public class Company {

    static private Employee employees[] = new Employee[10];

    public static void main(String[] args) {
        Employee e1 = new EmployeeRate("John", 100);
        Employee e2 = new EmployeeHour("Ben", 15000);
        Employee e3 = new EmployeeHour("Mike", 800.5);
        Employee e4 = new EmployeeRate("Den", 1200.14);

        hireEmployee(e1);
        hireEmployee(e2);
        hireEmployee(e3);
        hireEmployee(e4);

        sortBySalary();
        System.out.println("Sorted by Monthly pay");
        showReport();
        fireEmployee(e2);
        showReport();
    }

    public static void hireEmployee(Employee e) {
        if (e.isInStaff() == false) {
            e.setInStaff(true);
            employees[e.getId()] = e;
            System.out.println(e.getName() + " was hired in staff");
        } else {
            System.out.println(e.getName() + " have already worked");
        }
    }

    public static void fireEmployee(Employee fireCandidate) {
        for (int index = 0; index < employees.length; index++) {
            if (fireCandidate.equals(employees[index])) {
                System.out.println(fireCandidate.getName() + " was fired");
                employees[index] = null;
            }
        }
    }

    public static void sortBySalary() {
        for (int index = 0; index < employees.length - 1; index++) {
            for (int nextIndex = index + 1; nextIndex < employees.length; nextIndex++) {
                if (employees[index] != null && employees[nextIndex] != null) {
                    double indexSalary = employees[index].calculateSalary();
                    double nextIndexSalary = employees[nextIndex].calculateSalary();
                    compareSalary(index, nextIndex, indexSalary, nextIndexSalary);
                }
            }
        }
    }

    public static void showReport() {
        Employee emptyRecord = null;
        for (Employee e : employees) {
            if (e != emptyRecord) {
                System.out.println(e.toString());
                System.out.println("-----------");
            }
        }
    }

    private static void compareSalary(int index, int nextIndex, double indexSalary, double nextIndexSalary) {
        if (indexSalary < nextIndexSalary) {
            Employee template = employees[index];
            employees[index] = employees[nextIndex];
            employees[nextIndex] = template;
        }
    }
}


